package com.sgb.kjwl.view.ui.activity.transport.company;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.swgk.core.event.MessageEvent;
import com.sgb.kjwl.databinding.ActivityCargoManageBinding;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.transport.AddMenuPopWindow;
import com.sgb.kjwl.view.ui.adapter.transport.CargoManageHomeTabAdapter;
import com.sgb.kjwl.view.ui.fragment.transport.company.LinkedFleetFragment;
import com.sgb.kjwl.view.ui.fragment.transport.company.ProprietaryFleetFragment;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * author:熊丽
 * createDate:2021/10/9 10:19
 * introduce:2.1.3新增企业车队管理
 * modifier:xxx
 * modifiyDate:
 * modifiyContent:
 */
public class FleetManagementActivity extends AppActivity implements AddMenuPopWindow.MenuClickResult{
    ActivityCargoManageBinding binding;
    private boolean mIsGeneralGoods = true;
    //顶部tab 复用货源管理同一布局
    private CargoManageHomeTabAdapter tabAdapter;
    private AddMenuPopWindow mAddPopWindow;

    //跳转操作
    public static void start(Context context, boolean isGeneralGoods) {
        Intent intent = new Intent(context, FleetManagementActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isGeneralGoods);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_manage);
        binding.rightView.setText("添加车队");
        binding.setLifecycleOwner(this);
        binding.rightView.setOnClickListener(this);
        if (mAddPopWindow == null) {
            mAddPopWindow = new AddMenuPopWindow(this, 1);
            mAddPopWindow.setClickResult(this);
        }
        initTab();
        initPager();
        initEvent();
        mIsGeneralGoods = getIntent().getBooleanExtra(RouterKey.BOOLEAN, true);
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("自营车队");
        tabs.add("挂靠车队");
        tabAdapter = new CargoManageHomeTabAdapter(tabs);
        binding.rvTab.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.rvTab.setAdapter(tabAdapter);
    }

    private void initPager() {
        //设置加载当前页面左右两页内容
        binding.viewpager.setOffscreenPageLimit(1);
        //viewPager适配器
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(ProprietaryFleetFragment.newInstance(false));
        fragments.add(LinkedFleetFragment.newInstance(true));
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), fragments);
        binding.viewpager.setAdapter(pagerAdapter);
    }

    private void initEvent() {
        binding.ivBack.setOnClickListener(v -> finish());
        //
        tabAdapter.setOnItemClickListener((adapter, view, position) -> {
            tabAdapter.setSelectIndex(position);
            binding.viewpager.setCurrentItem(position);
        });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabAdapter.setSelectIndex(position);
                mIsGeneralGoods = (position == 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_view:
                mAddPopWindow.showAsDropDown(binding.rightView, -10, -20);
                break;

        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void fundAccountEvent(MessageEvent event) {
        //开通资金账户后刷新用户数据
        if (event.code == EventConstant.EVENT_REFRESH) {
            UserOperating.getInstance().refresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onResult(int position) {
        if (position==0){
            AddProprietaryFleetActivity.start(getActivity(),null);
        }else if (position==1){
            InvitationFleetActivity.start(getActivity());
        }
    }
}
